package com.atguigu.srb.core.controller.admin;

import com.atguigu.srb.common.exception.Assert;
import com.atguigu.srb.common.result.R;
import com.atguigu.srb.common.result.ResponseEnum;
import com.atguigu.srb.core.pojo.entity.IntegralGrade;
import com.atguigu.srb.core.service.IntegralGradeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/admin/core/integralGrade")
@Api(tags = "积分等级管理")
@Slf4j
public class AdminIntegralGradeController {
    @Resource
    private IntegralGradeService integralGradeService;

    @ApiOperation("获取所有积分等级列表")
    @GetMapping("list")
    public R getList() {
        List<IntegralGrade> list = integralGradeService.list();
        return R.OK().data("list", list);
    }

    @ApiOperation("根据id获取积分等级")
    @GetMapping("getById/{id}")
    public R getById(@ApiParam(value = "积分等级id") @PathVariable Long id) {
        IntegralGrade integralGrade = integralGradeService.getById(id);
        Assert.notNull(integralGrade, ResponseEnum.INTEGRAL_GRADE_NULL_ERROR);
        return R.OK().data("integralGrade", integralGrade);
    }

    // 添加removeById方法
    @ApiOperation("/根据id删除积分等级")
    @DeleteMapping("remove/{id}")
    public R removeById(@ApiParam(value = "积分等级id") @PathVariable Long id) {
        boolean result = integralGradeService.removeById(id);
        Assert.isTrue(result, ResponseEnum.DELETE_ERROR);
        return R.OK().message("删除成功");
    }

    @ApiOperation("/新增积分等级")
    @PostMapping("save")
    public R saveIntegralGradeService(@RequestBody IntegralGrade integralGrade) {
        BigDecimal borrowAmount = integralGrade.getBorrowAmount();
        Assert.isTrue(borrowAmount.intValue() != 0, ResponseEnum.BORROW_AMOUNT_NULL_ERROR);

        boolean result = integralGradeService.save(integralGrade);
        Assert.isTrue(result, ResponseEnum.SAVE_ERROR);
        return R.OK().message("保存成功");
    }

    @ApiOperation("/更新积分等级")
    @PutMapping("update")
    public R updateById(@RequestBody IntegralGrade integralGrade) {
        boolean result = integralGradeService.updateById(integralGrade);
        Assert.isTrue(result, ResponseEnum.UPDATE_ERROR);
        return R.OK().message("修改成功");
    }
}